/**
 * URL参数处理工具类
 * @example
 * // 获取单个参数：UrlUtil.getParam('code')
 * // 获取所有参数：UrlUtil.getAllParams()
 * // 添加参数：UrlUtil.addParams({code: '123'})
 */
const UrlUtil = {
    getParam(name, url = window.location.href) {
        const regex = new RegExp(`[?&]${name}=([^&#]*)`);
        const results = regex.exec(url);
        return results ? decodeURIComponent(results[1]) : null;
    },

    getAllParams(url = window.location.href) {
        const params = {};
        new URL(url).searchParams.forEach((value, key) => params[key] = value);
        return params;
    },

    addParams(params, url = window.location.href) {
        const urlObj = new URL(url);
        Object.keys(params).forEach(key => urlObj.searchParams.append(key, params[key]));
        return urlObj.toString();
    }
};

// 如果使用 CommonJS
if (typeof module !== 'undefined' && module.exports) {
    module.exports = UrlUtil;
} 